﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Radiation___IM.Commands
{
    public class Nick : ICommand
    {
        private readonly string m_commandkey = "nick";

        public Nick()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return "change_nick";
        }

        public void execute(string[] command_parts, Client client)
        {
            if (command_parts.Length > 1)
            {
                if (((Radiation___IM.Channel)ChannelManager.Instance.Channels["default"]).getClient(command_parts[1]) == null &&
                    Users.UserManager.Instance[command_parts[1]] == null)
                {
                    if (!command_parts[1].StartsWith("guest") && !command_parts[1].StartsWith("system") && !command_parts[1].Contains(' '))
                    {
                        if (command_parts[1].Length > 1)
                        {
                            changeNick(command_parts[1], client);
                        }
                        else
                            MessageExecution.sendData(client, "You need at least 2 characters for your nickname");
                    }
                    else
                        MessageExecution.sendData(client, "The nickname " + command_parts[1] + " looks like a reserved nickname and is not allowed.");
                }
                else
                    MessageExecution.sendData(client, "The nickname " + command_parts[1] + " is already in use");
            }
            else
                MessageExecution.sendData(client, "You have to send a valid nickname");
        }

        public static void changeNick(string nick_name, Client client)
        {
            MessageExecution.NoticeChannels(client, client.Nickname + " has changed his nickname to " + nick_name);
            string old_nick = client.Nickname;
            client.Nickname = nick_name;
            MessageExecution.sendData(client, "Your new nickname is : " + nick_name);
            Thread.Sleep(5);
            if (!client.Invisible)
                MessageExecution.NoticeChannelsAPI(client, old_nick + ";" + client.Nickname, Radiation.Common.Communication.APIMethods.CLIENT_RENAME_USER);
            Thread.Sleep(5);
            MessageExecution.sendDataAPI(client, client.Nickname, "user.nickname");
            MessageExecution.sendDataAPI(client, "", Radiation.Common.Communication.APIMethods.CLIENT_CLEAR_FRIEND_LIST);

            if (Users.UserManager.Instance[client.Nickname] != null && 
                Users.UserManager.Instance[client.Nickname].Friends.Count > 0)
            {
                MessageExecution.sendDataAPI(client, 
                    Users.UserManager.Instance[client.Nickname].serializeFriends(), 
                    Radiation.Common.Communication.APIMethods.GET_FRIEND_LIST);
            }
        }
    }
}
